﻿#include "KeyBoard.h"


#include <QRegExp>

KeyBoard::KeyBoard(QWidget *parent) : QDialog(parent)
{
    labelHintChangeKW = new QLabel(this);
    labelHintChangeKW->move(10,10);
    labelHintChangeKW->resize(120, 20);
    labelHintChangeKW->setText(tr("请选择调节方式:"));

    radioButtonPercent = new QRadioButton(this);
    radioButtonPercent->move(10, 40);
    radioButtonPercent->resize(70, 40);
    radioButtonPercent->setText(tr("百分比"));
    radioButtonPercent->setChecked(true);

    radioButtonAbsolute = new QRadioButton(this);
    radioButtonAbsolute->move(130, 40);
    radioButtonAbsolute->resize(70, 40);
    radioButtonAbsolute->setText(tr("绝对值"));

    lineEdit = new QLineEdit(this);
    lineEdit->move(10, 90);
    lineEdit->resize(180, 40);
    lineEdit->setStyleSheet("QLineEdit{font-size:20px; }");
    lineEdit->setMaxLength(4);
    lineEdit->setReadOnly(true);
    lineEdit->setAlignment(Qt::AlignRight);



    QPushButton* buttonConfirm = new QPushButton(this);
    buttonConfirm->setGeometry(195, 90, 70, 40);
    buttonConfirm->setText(tr("确定"));
    buttonConfirm->setStyleSheet("QPushButton{outline: none; border-radius: 5px; font-weight: bold; font-size: 23px; color: rgb(25, 25, 112); background-color:rgb(171, 223, 245); }");

    connect(buttonConfirm, SIGNAL(clicked()), this, SLOT(onSwitchParameter()));

    QPushButton* button[12];

    const char* data[12] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "清除", "0","退出"};

    for(int i=0; i<3; i++)
    {
        for(int j=0; j<4; j++)
        {
            button[j] = new QPushButton(this);
            button[j]->move(10 + (60+5)*j, 135+(50+5)*i);
            button[j]->resize(60, 50);
            button[j]->setText(data[i*4 + j]);
            button[j]->setStyleSheet("QPushButton{outline: none; border-radius: 5px; font-weight: bold; font-size: 23px; color: rgb(25, 25, 112); background-color:rgb(171, 223, 245); } QPushButton:pressed{background-color: rgb(25, 25, 112) ; color: white;}");
            button[j]->setFocusPolicy(Qt::NoFocus);
            connect(button[j], SIGNAL(clicked()), this, SLOT(onHandleKeyborad()));
        }
    }
    this->hide();
}

void KeyBoard::onSwitchParameter()
{
    char ctrlType = 0;
    QString m_data = lineEdit->text();

    if( !m_data.isEmpty() )
    {
        if( radioButtonPercent->isChecked() )
        {
            QRegExp regExp("\\d{1,2}");
            if( regExp.exactMatch(m_data) )
            {
                ctrlType = 0;
                emit sendParameter(m_data, ctrlType);
                close();
                lineEdit->clear();
            }
            else
            {
                lineEdit->setText(tr("数据错误!"));
            }
        }
        else if( radioButtonAbsolute->isChecked() )
        {
            QRegExp regExp("(\\d{1,3})|(1000)");
            if( regExp.exactMatch(m_data) )
            {
                ctrlType = 1;
                emit sendParameter(m_data, ctrlType);
                close();
                lineEdit->clear();
            }
            else
            {
                lineEdit->setText(tr("数据错误!"));
            }
        }
    }
}

void KeyBoard::onHandleKeyborad()
{
    QPushButton* btn = qobject_cast<QPushButton*>(sender());

    if(btn->text() == "退出")
    {
        hide();
        lineEdit->clear();
    }
    else if( btn->text() == "清除" )
    {
        lineEdit->backspace();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}
